/*
** readdir.c for tp in /home/comet_a/afs/local/sys_unix/my_ls/tp_opendir_readdir
** 
** Made by alexandre comet
** Login   <comet_a@epitech.net>
** 
** Started on  Wed Nov 30 16:57:26 2011 alexandre comet
** Last update Mon Oct 29 16:16:59 2012 thomas nadalie
*/

#include	<dirent.h>
#include	<stdlib.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<unistd.h>

void		my_putchar(char c)
{
  write(1, &c, 1);
}

void		my_putstr(char *str)
{
  int		i;

  i = 0;
  while (str[i])
    my_putchar(str[i++]);
}

int		main(int ac, char **av)
{
  DIR		*dirp;
  struct dirent	*d;
  const	char	*name;
  char		buff[4096];
  struct stat	sb;

  name = av[1];
  dirp = opendir(name);
  printf("%s\n", buff);
  if (dirp == NULL)
    exit (0);
  d = readdir(dirp);
  if (d == NULL)
    exit (0);
  my_putstr(d->d_name);
  my_putchar('\n');
  while (d != 0)
    {
      if (stat(d->d_name, &sb) == -1)
	{
	  my_putstr("stat func failed\n");
	  return (-1);
	}
      d = readdir(dirp);
      if (d == NULL)
	exit (0);
      my_putstr(d->d_name);
      printf("  :%d\n", sb.st_atime);

      my_putchar('\n');
    }
}
